#include "unp.h"

int main(int argc, char** argv){
	struct sockaddr_in servaddr;
	int sockfd;

	if(argc != 2)
		err_quit("Usage: daytimetcpcli <ip-address>");

	if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
		err_sys("socket error");

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port  = htons(13);
	if( inet_aton(argv[1], &servaddr.sin_addr) < 0)
		err_sys("inet_aton error");

	if( connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0)
		err_sys("connect error");

	char recvline[MAXLINE + 1];
	int n;
	while( (n = read(sockfd, recvline, MAXLINE)) > 0){
		recvline[n] = 0;
		fputs(recvline, stdout);
	}
	if(n < 0)
		err_sys("read error");
	return 0;
}
